<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>今天我是大哥</title>

    <style>
    </style>
</head>

<body>

</body>
<script>
    var str = "aabaaf"
    var s = str.split("")
    console.log(getNext(s));
    // 构建next表
    function getNext(str) {
    // 初始化
    let next = new Array(str.length).fill(0);
    // 循环遍历str
    for (let i = 1, j = 0; i < str.length; i++) {
        // 不相等的话就回退j
        while (j > 0 && str[j] != str[i]) {
            j = next[j - 1];
        }
        if (str[i] == str[j]) {
            next[i] = ++j;
        }
    }
    return next;
}
</script>

</html>